//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "CBaseProgramShell.h"
#include "../../Systems/AllSystems.h"

namespace Engine
{
namespace Resources
{

	static	CMetaClass		s_cMetaUBO;
	static	CMetaClass		s_cMetaTBO;
	static	CMetaClass		s_cMetaSWB;
	static	CMetaClass		s_cMetaProgramTexture;
	static	CMetaClass		s_cMetaProgram;


	_CREATE_META_OBJECT( TUBOInit,		s_cMetaUBO );
	_CREATE_META_OBJECT( TTBOInit,		s_cMetaTBO );
	_CREATE_META_OBJECT( TSWBInit,		s_cMetaSWB );
	_CREATE_META_OBJECT( TTextureInit,	s_cMetaProgramTexture );
	_CREATE_META_OBJECT( TProgramInit,	s_cMetaProgram );

	

	CBaseProgramShell::CBaseProgramShell(const char *pFileName):
		CResource(pFileName), _eProgType(e_program::UNKNOWN), _eLayerBits(e_render_layer::UNKNOWN_BITS)
	{
		ASSERT( _s_IsInitMetaData() );
	}

	CBaseProgramShell::~CBaseProgramShell()
	{
	}

	const CMetaClass * CBaseProgramShell::_s_GetProgram()
	{
		return &s_cMetaProgram;
	}
	
	bool CBaseProgramShell::_s_IsInitMetaData()
	{
		return s_cMetaProgram.IsInit();
	}

	bool CBaseProgramShell::_s_InitMetaData()
	{
		CMetaAddIterator	*iter = nullptr;


		if ( !s_cMetaUBO.IsInit() )
		{
			TUBOInit	s_ubo;

			s_cMetaUBO.Create( "UBO", sizeof(s_ubo) );

			iter = s_cMetaUBO.GetAddIter( &s_ubo );
			iter->AddField( &s_ubo.sName,			"name"	);
			iter->AddField( &s_ubo.eType,			"type"	);
			iter->Free();
		}

		if ( !s_cMetaTBO.IsInit() )
		{
			TTBOInit	s_tbo;

			s_cMetaTBO.Create( "TBO", sizeof(s_tbo) );

			iter = s_cMetaTBO.GetAddIter( &s_tbo );
			iter->AddField( &s_tbo.sUnifName,		"name"			);
			iter->AddField( &s_tbo.sUnifCountName,	"counter_name"	);
			iter->AddField( &s_tbo.uElemSize,		"element_size"	);
			iter->AddField( &s_tbo.eType,			"type"			);
			iter->AddField( &s_tbo.eFormat,			"format"		);
			iter->Free();
		}

		if ( !s_cMetaSWB.IsInit() )
		{
			TSWBInit	s_swb;

			s_cMetaSWB.Create( "SoftwareBuffer", sizeof(s_swb) );

			iter = s_cMetaSWB.GetAddIter( &s_swb );
			iter->AddField( &s_swb.eType,			"type"			);
			iter->AddField( &s_swb.uElemSize,		"element_size"	);
			iter->Free();
		}

		if ( !s_cMetaProgramTexture.IsInit() )
		{
			TTextureInit	s_tex;

			s_cMetaProgramTexture.Create( "ProgramTexture", sizeof(s_tex) );

			iter = s_cMetaProgramTexture.GetAddIter( &s_tex );
			iter->AddField( &s_tex.sName,			"name"		);
			iter->AddField( &s_tex.eType,			"type"		);
			iter->AddField( &s_tex.eFlags,			"sampler"	);
			iter->Free();
		}

		if ( !s_cMetaProgram.IsInit() )
		{
			TProgramInit		s_prog;

			s_cMetaProgram.Create( "Program", sizeof(s_prog) );

			iter = s_cMetaProgram.GetAddIter( &s_prog );
			iter->AddField( &s_prog.eType,				"type"					);
			iter->AddField( &s_prog.eLayerBits,			"layer_bits"			);
			iter->AddField( &s_prog.aUniforms,			"uniforms"				);
			iter->AddField( &s_prog.aTexInit,			"textures"				);
			iter->AddField( &s_prog.aUBOInit,			"ubo_data"				);
			iter->AddField( &s_prog.aTBOInit,			"tbo_data"				);
			iter->AddField( &s_prog.aSWBInit,			"swb_data"				);
			iter->Free();
		}

		return true;
	}

//-------------------------------------------------------------------

}	// Resources
}	// Engine